package com.hoshiicloud.saleorder.controller.refund;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hoshiicloud.common.controller.BaseController;
import com.hoshiicloud.common.query.OmnipotentParam;
import com.hoshiicloud.common.query.OmnipotentWrapper;
import com.hoshiicloud.common.rpc.Response;
import com.hoshiicloud.saleorder.entity.RefundOrder;
import com.hoshiicloud.saleorder.param.refund.RefundOrderListParam;
import com.hoshiicloud.saleorder.param.refund.RefundOrderPageParam;
import com.hoshiicloud.saleorder.service.refund.RefundOrderService;
import com.hoshiicloud.saleorder.vo.refund.RefundOrderVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 退款单(RefundOrder)表控制层
 *
 * @author qry
 * @since 2019-08-22 11:00:16
 */
@RestController
@RequestMapping("/refund/refund-order")
@Slf4j
@Api(value = "退款单", tags = {"退款单"})
public class RefundOrderController extends BaseController {
    /**
     * 服务对象
     */
    @Autowired
    private RefundOrderService refundOrderService;

    @PostMapping("/getPage")
    @ApiOperation(value = "分页查询退款单", notes = "分页查询退款单")
    public Response<Page<RefundOrderVo>> getPage(@RequestBody RefundOrderPageParam param){
        QueryWrapper<RefundOrder> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda()
                .eq((null!= param.getStoreId() &&  param.getStoreId()!=0),RefundOrder::getStoreId,param.getStoreId())
                .eq(StringUtils.isNotBlank(param.getRefundNo()), RefundOrder::getRefundNo, param.getRefundNo())
                .like(StringUtils.isNotBlank(param.getProductTitle()), RefundOrder::getProductTitle, param.getProductTitle())
                .like(StringUtils.isNotBlank(param.getOrderNo()), RefundOrder::getOrderNo, param.getOrderNo())
                .like(StringUtils.isNotBlank(param.getMainNo()), RefundOrder::getMainNo, param.getMainNo())
                .eq((param.getServiceType()!=null && param.getServiceType()!= 0),RefundOrder::getServiceType,param.getServiceType())
                .eq((param.getRefundType()!=null && param.getRefundType()!= 0),RefundOrder::getRefundType,param.getRefundType())
                .eq((null!= param.getStatus() && param.getStatus()!= 0),RefundOrder::getStatus, param.getStatus())
                .eq((null!= param.getUserId() && param.getUserId()!= 0),RefundOrder::getUserId,param.getUserId())
                .eq((null!= param.getSubId()&& param.getSubId()!= 0),RefundOrder::getSubId,param.getSubId());
        queryWrapper.ge( StringUtils.isNotBlank(param.getBeginTime()),"left(created_time,19)",param.getBeginTime());
        queryWrapper.le( StringUtils.isNotBlank(param.getEndTime()),"left(created_time,19)",param.getEndTime());
        queryWrapper.orderByDesc("created_time");
        Page<RefundOrder> page = new Page<>(Long.parseLong(param.getPageNumber()), Long.parseLong(param.getPageSize()));
        String key = "RefundOrderPageParam{"+param.toString() + "},Page{pageNumber=" + param.getPageNumber() + ",pageSize=" + param.getPageSize() + "}";
        return Response.data(refundOrderService.getPage(page, queryWrapper, key));
    }

    @GetMapping("/{id}")
    public Response<RefundOrder> getById(@PathVariable("id") Long id){
        return Response.data(refundOrderService.getRefundOrderById(id));
    }

    @PostMapping("/save")
    @ApiOperation(value = "新增退款单", notes = "新增退款单")
    public Response<RefundOrder> save(@RequestBody RefundOrder entity){
        return  Response.data(refundOrderService.saveRefundOrder(entity));
    }

    @PutMapping("/update")
    @ApiOperation(value = "编辑退款单byID", notes = "编辑退款单byID")
    public Response<Boolean> updateById(@RequestBody RefundOrder entity){
        return  Response.data(refundOrderService.updateRefundOrder(entity));
    }


    @PostMapping("/list")
    @ApiOperation(value = "根据条件获取该子订单下的所有退款单", notes = "根据条件获取该子订单下的所有退款单")
    public Response<List<RefundOrderVo>> listRefundOrder(@RequestBody RefundOrderListParam param){
        String key = param.toString();
        QueryWrapper<RefundOrder> q = new QueryWrapper<>();
        q.lambda().eq((null!= param.getSubId()),RefundOrder::getSubId,param.getSubId())
                .eq((null!= param.getStatus()),RefundOrder::getStatus, param.getStatus())
                .eq((null!= param.getUserId()),RefundOrder::getUserId,param.getUserId())
                .ne((null!= param.getNeStatus()),RefundOrder::getStatus,param.getNeStatus());
        return Response.data(refundOrderService.listRefundOrder(q,key));
    }

    @ApiOperation(value = "根据自定义条件查询退款单信息")
    @PostMapping("/omniRefundOrder")
    public Response omniRefundOrder(@RequestBody List<OmnipotentWrapper> wrappers) {
        String key = wrappers.toString();
        List<RefundOrder> orderMains = refundOrderService.omniRefundOrder(wrappers,key);
        return Response.data(orderMains);
    }



    @PutMapping("/updateByParam")
    @ApiOperation(value = "根据条件更新退款单信息")
    public Response<Boolean> updateByParam(@RequestBody OmnipotentParam<RefundOrder> param){
        return Response.data(refundOrderService.updateByParam(param));
    }
}
